<script setup lang="ts">
import { onMounted } from 'vue'
import { loginThirdparty, loginBinding } from '@/api/user'
import { ref } from 'vue'
import { showSuccessToast, type FieldRule, type FormInstance } from 'vant'
import { useMobileCode } from '@/composable/index'
import { useRouter } from 'vue-router'
import { useUser } from '@/stores'
import { useRoute } from 'vue-router'
const mobile = ref('13616161616')
const code = ref('')
let openIdVal = ''
// 表单验证规则
const rules = {
  mobile: [
    { required: true, message: '必填', trigger: 'onChange' },
    { pattern: /^1[3-9]\d{9}$/, message: '请正确输入手机号', trigger: 'onChange' }
  ] as FieldRule[],
  code: [
    { required: true, message: '必填', trigger: 'onChange' },
    { pattern: /^\d{6}$/, message: '请输入六位验证码', trigger: 'onChange' }
  ] as FieldRule[]
}
const form = ref<FormInstance>()
// 发送验证码
// const timeTotal = ref(0)
// let timeId = -1
const { timeTotal, cb } = useMobileCode(mobile, 'bindMobile')
const sendCode = async () => {
  await form.value!.validate('mobile')
  cb()
  //   console.log('验证码', res)
}
// 绑定按钮点击
const router = useRouter()
const store = useUser()
const route = useRoute()
const submit = async () => {
  await form.value!.validate()
  const res = await loginBinding({
    code: code.value,
    mobile: mobile.value,
    openId: openIdVal
  })
  //   存储token
  // 跳转页面
  // 存储用户信息
  store.setUserInfo(res.data)
  // 提示
  showSuccessToast('登录成功')
  // 路由跳转
  //   A==>login===C    A    B跳C用replace:替换当前路由，后退时不会存在上一路由
  router.replace((route.query.redirect as string) || '/')
  console.log('绑定结果', res)
}
const show = ref(false)
onMounted(() => {
  // 检查是否扫码登陆了
  if (window.QC.Login.check()) {
    window.QC.Login.getMe(async openId => {
      openIdVal = openId
      console.log('openId', openId)
      try {
        const res = await loginThirdparty({
          openId: openId,
          source: 'qq'
        })
        // 绑定了
        // 存储用户信息
        store.setUserInfo(res.data)
        // 提示
        showSuccessToast('登录成功')
        // 路由跳转
        //   A==>login===C    A    B跳C用replace:替换当前路由，后退时不会存在上一路由
        router.replace((route.query.redirect as string) || '/')
        console.log('是否绑定', res)
      } catch {
        show.value = true
        // 没绑定
        /*
        1：表单验证
        2：收验证码
        3：倒计时
        4：实现绑定
        */
      }
    })
  }
})
</script>

<template>
  <div class="login-page" v-if="show">
    <cp-nav-bar></cp-nav-bar>
    <div class="login-head">
      <h3>手机绑定</h3>
    </div>
    <van-form autocomplete="off" ref="form">
      <van-field
        :rules="rules.mobile"
        v-model="mobile"
        name="mobile"
        placeholder="请输入手机号"
      ></van-field>
      <van-field :rules="rules.code" v-model="code" name="code" placeholder="请输入验证码">
        <template #button>
          <span class="btn-send" v-if="timeTotal === 0" @click="sendCode">发送验证码</span>
          <span class="btn-send" v-else>{{ timeTotal }}秒后发送验证码</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-button block round type="primary" @click="submit"> 立即绑定 </van-button>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-icon {
    margin-right: 10px;
  }
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
